package com.ruoyi.web.controller.agoniapp;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.JwtUtils;
import com.ruoyi.common.utils.TokenUser;
import com.ruoyi.common.utils.UserIdByRequest;
import com.ruoyi.system.pojo.entity.Event;
import com.ruoyi.system.pojo.vo.AllConditionEventVO;
import com.ruoyi.system.pojo.vo.MyPublishEventVO;
import com.ruoyi.system.service.IAgoniEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/agoniapp/event")
public class EventController {

    @Autowired
    private IAgoniEventService agoniEventService;

    @GetMapping("/listEvents")
    public AjaxResult listEvents() {
        return AjaxResult.success(agoniEventService.listEvents());
    }


    @GetMapping("/getEventsByFacilityId/{facilityId}")
    public AjaxResult getEventsByFacilityId(@PathVariable Long facilityId) {
        return AjaxResult.success(agoniEventService.getEventsByFacilityId(facilityId));
    }

    @GetMapping("/listAllConditionEvents")
    public AjaxResult getAllConditionEvents() {
        return AjaxResult.success(agoniEventService.listAllConditionEvents());
    }


    @PostMapping("/addEvent")
    public AjaxResult addEvent(@RequestBody Event event, HttpServletRequest request) {


        Long userId = UserIdByRequest.getUserId(request);
        int result = agoniEventService.addEvent(event, userId);

        return AjaxResult.success("添加成功");
    }


    @GetMapping("/getEventByReceiverId")
    public AjaxResult getEventByReceiverId(HttpServletRequest request) {
//传递参数
        Long userId = UserIdByRequest.getUserId(request);
        return AjaxResult.success("获取成功", agoniEventService.getEventByReceiverId(userId));
    }

    @PutMapping("/updateReceiveEvent/{eventId}")
    public AjaxResult updateReceiveEvent(@PathVariable Long eventId, HttpServletRequest request) {
        //传递参数
       //获取token中的用户id
        Long userId = UserIdByRequest.getUserId(request);

        int result=agoniEventService.updateReceiveEvent(eventId,userId);
        if(result==0){
            return AjaxResult.error("暂不能接取此任务");
            }

        return AjaxResult.success("接取成功");

    }

    //获取客户发布的事件
    @GetMapping("getMyPublishEvent")
    public AjaxResult getMyPublishEvent(HttpServletRequest request) {
        //获取token中的用户id
        Long userId = UserIdByRequest.getUserId(request);
        List<MyPublishEventVO> myPublishEventVOList = agoniEventService.getMyPublishEventList(userId);

        return AjaxResult.success("获取成功",myPublishEventVOList);
    }

    //接收任务的完成任务提交
    @PutMapping("completeEvent/{eventId}")
    public AjaxResult completeEvent(@PathVariable Long eventId,HttpServletRequest request) {
        //获取token中的用户id
        Long userId = UserIdByRequest.getUserId(request);
        int result = agoniEventService.completeEvent(eventId);
        if(result==0){
            return AjaxResult.error("任务提交失败,请稍后再试");
        }
        return AjaxResult.success("任务完成,等待任务发布者确认");
    }

    //获取除自己以外的发布者发布的事件
    @GetMapping("listAllConditionEventsWithoutSelf")
    public AjaxResult getOtherPublishEvent(HttpServletRequest request) {
        //获取token中的用户id
        Long userId = UserIdByRequest.getUserId(request);
        List<AllConditionEventVO> allConditionEventVOList = agoniEventService.listAllConditionEventsWithoutSelf(userId);

        return AjaxResult.success("获取成功",allConditionEventVOList);
    }

}